import glob
import os
import time
import shutil
import pandas as pd
from GetDataOnly import get_data_only
from 判断是否有广告弹窗 import yes_or_no
from 筛选excel import excel
from login import login_ing
from page import get_page
from 获取无效id import get_wuxiaoid


def get_data(port, user_path, down_path, date, usernames):
    # 清空下载文件夹
    for root, dirs, files in os.walk(down_path):
        # 删除文件
        for file in files:
            file_path = os.path.join(root, file)
            os.remove(file_path)
    for x in range(100):
        page = get_page(port, user_path, down_path)
        tab_id = page.get_tab()
        try:
            page.get("https://fxg.jinritemai.com/ffa/multiShop")
            time.sleep(5)
            if page('.auxo-btn auxo-btn-primary', timeout=2):
                page.quit()
                page.wait(3)
                page = get_page(port, user_path, down_path)
                page.get("https://fxg.jinritemai.com/login/common")
            if not page.eles(".index_roleItem__1-Hwe", timeout=1) and not (
                    page('.feige_headerIcon__3CdVk', timeout=1) or page.eles('接待', timeout=1)):
                login_ing(page, usernames)
            if page.s_eles('text=重试'):
                print('出现重试，登录信息异常')
                page.quit()
                continue
            page.wait.ele_deleted('text=加载中', timeout=30)
            店铺eles = page.eles('@class^index_introName')
            店铺名_list = [i.text for i in 店铺eles]
            for 店铺名 in 店铺名_list:
                page.wait.ele_deleted('text=加载中', timeout=10)
                shop_name = '抖音小店-' + 店铺名
                # 如果存在该店铺要获取的数据文件则跳过该店铺
                if os.path.exists(f"C:\\抖音客服效率统计\\{date}\\{shop_name}.xlsx"):
                    continue
                print(shop_name)
                page(店铺名).click(by_js=True)
                page.wait.load_start()
                page.wait.doc_loaded()
                if page.eles('.feige_headerIcon__3CdVk', timeout=5):
                    page('.feige_headerIcon__3CdVk').click(by_js=True)
                elif page.eles('接待', timeout=2):
                    page('接待').click(by_js=True)
                else:
                    print('没有找到飞鸽入口')
                    df = pd.DataFrame()
                    df.to_excel(f"C:\\抖音客服效率统计\\{date}\\{shop_name}.xlsx")
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                page.wait(8)
                # 切换到最新打开的标签页
                tab = page.get_tab(page.latest_tab)
                yes_or_no(tab, page)
                tab("text=数据").click()
                page.wait(1)
                tab('text=历史会话').click()
                tab.wait(2)
                tab('text=更多筛选 ').click(by_js=True)
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[5].click()
                tab('.sp-select-all').click()
                ele = tab('.rc-virtual-list-holder-inner')
                ele.ele('text=人工会话，用户主动离开').click()
                ele.ele('text=人工会话，客服结束会话').click()
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[5].click()
                tab.wait(1)
                # 取消勾选未评价
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[9].ele('.auxo-select-selection-item-remove').click()
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[11].ele('.auxo-select-selection-item-remove').click()
                # 会话时间‘工作时间’
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[13].click()
                tab.wait(1)
                tab.eles('.rc-virtual-list-holder-inner')[1].ele('工作时间').click()
                # 选择人工接待‘是’
                tab.eles('.Mvg3OH8ZtQ7l4K_FCoZv')[6].click()
                tab.wait(1)
                tab.eles('.rc-virtual-list-holder-inner')[2].ele('是').click()
                # 输入时间
                tab('.auxo-picker-clear').click()
                tab('@placeholder=开始日期').input(date, clear=True)
                tab('@placeholder=结束日期').input(date, clear=True)
                tab.actions.key_down('ENTER')
                tab('text=查询').click(by_js=True)
                tab.wait(4)
                if tab.s_ele('暂无数据'):
                    print('暂无数据')
                    df = pd.DataFrame()
                    df.to_excel(f"C:\\抖音客服效率统计\\{date}\\{shop_name}.xlsx")
                    page.close_tabs(tab)
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                tab('text=导出报表').click(by_js=True)
                tab.wait(2)
                tab('@placeholder=请输入文件名').input(date, clear=True)
                tab('text=确定').click()
                tab.wait.download_begin()
                tab.wait.downloads_done()  # 等待下载结束
                wuxiaoid_list = get_wuxiaoid(tab)
                # 获取文件夹中所有的.xlsx文件
                xlsx_files = glob.glob(os.path.join(down_path, "*.xlsx"))
                shutil.move(xlsx_files[0], f"C:\\抖音客服效率统计\\{date}\\{shop_name}.xlsx")
                excel(shop_name, date, f"C:\\抖音客服效率统计\\{date}\\{shop_name}.xlsx", wuxiaoid_list)
                page.close_tabs(tab)
                page.wait(1)
                page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.quit()
            return '成功'
        except Exception as e:
            print(e)
            page.close_tabs(tab_id, others=True)
            continue
    return '失败'
